from rest_framework import serializers
from ..models import ChatSession, ChatMessage, AIKnowledgeBase, UserFeedback


class ChatSessionSerializer(serializers.ModelSerializer):
    """对话会话序列化器"""
    preview = serializers.ReadOnlyField()
    
    class Meta:
        model = ChatSession
        fields = ['id', 'title', 'preview', 'created_at', 'updated_at', 'is_active']


class ChatMessageSerializer(serializers.ModelSerializer):
    """对话消息序列化器"""
    message_type_display = serializers.CharField(source='get_message_type_display', read_only=True)
    feature_display = serializers.CharField(source='get_feature_display', read_only=True)
    
    class Meta:
        model = ChatMessage
        fields = [
            'id', 'session', 'message_type', 'message_type_display',
            'content', 'feature', 'feature_display', 'created_at',
            'is_helpful', 'response_time', 'confidence_score'
        ]


class AIKnowledgeBaseSerializer(serializers.ModelSerializer):
    """AI知识库序列化器"""
    category_display = serializers.CharField(source='get_category_display', read_only=True)
    
    class Meta:
        model = AIKnowledgeBase
        fields = [
            'id', 'title', 'category', 'category_display',
            'content', 'keywords', 'priority', 'is_active',
            'created_at', 'updated_at'
        ]


class UserFeedbackSerializer(serializers.ModelSerializer):
    """用户反馈序列化器"""
    rating_display = serializers.CharField(source='get_rating_display', read_only=True)
    user_name = serializers.CharField(source='user.username', read_only=True)
    
    class Meta:
        model = UserFeedback
        fields = [
            'id', 'user', 'user_name', 'message', 'rating',
            'rating_display', 'comment', 'created_at'
        ]